﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class backmanage_API_A_Emailgroup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string editType = string.IsNullOrEmpty(Request.Form["edit"]) ? "" : Request.Form["edit"];
        switch (editType)
        {
            case "groupadd":
                {
                    returnStr = GroupAdd();
                    break;
                }
            case "grouplist":
                {
                    returnStr = GroupList();
                    break;
                }
            case "groupinfo":
                {
                    returnStr = GroupInfo();
                    break;
                }
            case "groupupdate":
                {
                    returnStr = GroupUpdate();
                    break;
                }
            case "groupdelete":
                {
                    returnStr = GroupDelete();
                    break;
                }
            default:
                {
                    break;
                }
        }
        Response.Write(returnStr);
    }
    protected string GroupAdd()
    {
        string returnStr = "false";
        string name = string.IsNullOrEmpty(Request["name"]) ? null : Request["name"];
        string createid = "admin";
        MODEL.EmailGroup model = new MODEL.EmailGroup();
        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(createid))
        {
            model.ECREATEID = createid;
            model.EGROUPNAME = name;
            bool flag = new BLL.EmailGroup().EmailGroupAdd(model);
            if (flag)
                returnStr = "true";
        }
        return returnStr;
    }
    protected string GroupList()
    {
        StringBuilder returnStr = new StringBuilder();
        DataSet ds = new BLL.EmailGroup().EmailGroupList();
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    returnStr.Append("<tr id=\"" + dr["Eid"] + "\">");
                    returnStr.Append("<td>"+dr["Egroupname"]+"</td>");
                    returnStr.Append("<td>" + dr["Ecreateid"] + "</td>");
                    returnStr.Append("<td><a href=\"javascript:void(0)\" title=\"update\">编辑</a><a href=\"javascript:void(0)\" title=\"delete\">删除</a></td>");
                    returnStr.Append("</tr>");
                }
            }
        }
        return returnStr.ToString();
    }
    protected string GroupInfo()
    {
        StringBuilder returnStr = new StringBuilder();
        int id = string.IsNullOrEmpty(Request["id"]) ? 0 : Convert.ToInt32(Request["id"]);
        if (!id.Equals(0))
        {
            DataSet ds = new BLL.EmailGroup().EmailGroupList(id);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    returnStr.Append("{");
                    returnStr.Append("\"name\":\"" + ds.Tables[0].Rows[0]["Egroupname"] + "\"");
                    returnStr.Append("}");
                }
            }
        }
        return returnStr.ToString();
    }
    protected string GroupUpdate()
    {
        string returnStr = "false";
        int id = string.IsNullOrEmpty(Request["id"]) ? 0 : Convert.ToInt32(Request["id"]);
        string name = string.IsNullOrEmpty(Request["name"]) ? null : Request["name"];
        if (!id.Equals(0) && !string.IsNullOrEmpty(name))
        {
            MODEL.EmailGroup model = new MODEL.EmailGroup();
            model.ID = id;
            model.EGROUPNAME = name;
            bool flag = new BLL.EmailGroup().EmailGroupUpdate(model);
            if (flag)
                returnStr = "true";
        }
        return returnStr;
    }
    protected string GroupDelete()
    {
        string returnStr = "false";
        int id = string.IsNullOrEmpty(Request["id"]) ? 0 : Convert.ToInt32(Request["id"]);
        if (!id.Equals(0))
        {
            bool flag = new BLL.EmailGroup().EmailGroupDelete(id);
            if (flag)
                returnStr = "true";
        }
        return returnStr;
    }
}
